<template>
  <t-dialog :header="header" :visible.sync="visible" :onClose="closeDialog" :closeOnOverlayClick="false"
    @confirm="onSubmit" :confirm-btn="disabled ? null : '确认'" width="60%" top="5%">
    <div style="height: 65vh">
      <t-form ref="form" :data="formData" :colon="true" labelWidth="100px" :rules="rules">
        <t-form-item label="系统名称" name="serviceId">
          <t-select :readonly="disabled"
            @change="(val, content) => { formData.serviceName = content.option.serviceName }"
            v-model="formData.serviceId" filterable clearable :keys="{ label: 'serviceName', value: 'id' }"
            :options="serviceOptions">
          </t-select>
        </t-form-item>
        <t-form-item label="类型" name="noticeType">
          <t-select :readonly="disabled" clearable filterable v-model="formData.noticeType"
            :options="dict.type.notice_type"></t-select>
        </t-form-item>
        <t-form-item label="标题" name="title">
          <t-input :readonly="disabled" v-model="formData.title" clearable></t-input>
        </t-form-item>
        <t-form-item label="内容" name="content">
          <t-textarea clearable :readonly="disabled" :autosize="{ minRows: 5 }" v-model="formData.content"
            placeholder="请输入内容" />
        </t-form-item>

        <t-form-item label="发布时间" name="publishTime">
          <t-date-picker :disabled="disabled" enable-time-picker clearable
            v-model="formData.publishTime"></t-date-picker>
        </t-form-item>
        <t-form-item label="附件上传" name="sysAttMainVo.sysAttMainIds">
          <file-upload :multiple="true" :disabled="disabled" :fileList="fileLists"
            v-model="formData.sysAttMainVo.sysAttMainIds"></file-upload>
        </t-form-item>
        <t-form-item label="是否置顶" name="pinned">
          <t-switch :disabled="disabled" size="large" v-model="formData.pinned" :label="['是', '否']"
            :customValue="['1', '0']"></t-switch>
        </t-form-item>
        <t-form-item label="是否公开" name="isOpen">
          <t-switch :disabled="disabled" size="large" v-model="formData.isOpen" :label="['是', '否']"
            :customValue="['1', '0']"></t-switch>
        </t-form-item>
        <t-form-item v-if="disabled" label="阅读量" name="readCount">
          {{ formData.readCount }}
        </t-form-item>
      </t-form>
    </div>
  </t-dialog>
</template>

<script>
import FileUpload from "@/components/FileUpload/index.vue"
import { TODAY_TIME_START } from '@/utils/date';
import { getFiles } from "@/api/common";
import dayjs from "dayjs";

export default {
  name: "add-edit-tzgg",
  dicts: ['notice_type'],
  components: {
    FileUpload
  },
  props: ['header', 'objId', 'parentId', 'disabled'],
  data () {
    return {
      visible: true,
      serviceOptions: [],
      fileLists: [],
      formData: {
        serviceId: '',
        serviceName: '',
        title: '',
        noticeType: '',
        content: '',
        pinned: '0',
        isOpen: '1',
        publishTime: TODAY_TIME_START,
        sysAttMainVo: {
          sysAttMainIds: []
        }
      },
      rules: {
        serviceId: [{ required: true, message: "子系统不能为空" }],
        noticeType: [{ required: true, message: "类型不能为空" }],
        title: [{ required: true, message: "标题不能为空" }],
        content: [{ required: true, message: "内容不能为空" }],
        publishTime: [{ required: true, message: "发布时间不能为空" }],
      }
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler (val) {
        if (val) {
          this.getObj();
        }
      }
    },
  },
  created () {
    this.getSysServices();
  },
  methods: {
    getSysServices () {
      this.$api.system.subsystem.sysServiceList({ page: 1, pageSize: 10000 }).then((res) => {
        this.serviceOptions = res.rows;
      }).catch((e) => {
        this.$message.error(e.toString());
      })
    },
    getObj () {
      this.$api.system.tzgg.get(this.objId).then(response => {
        response.data.sysAttMainVo = response.data.sysAttMainVo || { sysAttMainIds: [] }
        Object.assign(this.formData, response.data)
      }).catch((e) => {
        this.$message.error(e.toString());
      });
      getFiles(this.objId).then((res) => {
        this.fileLists = []
        const ids = [];
        if (res.data && res.data.length) {
          res.data.forEach((item) => {
            ids.push(item.id);
            this.fileLists.push({ name: item.fileName, url: item.fullFilePath, id: item.id, filePath: item.filePath })
          })
          this.formData.sysAttMainVo.sysAttMainIds = ids;
        }
      });
    },
    closeDialog () {
      this.$emit("update:visible", false);
    },
    onSubmit () {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          if (this.objId) {
            this.$api.system.tzgg.update(this.formData).then(() => {
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
            }).catch((e) => {
              this.$message.error(e.toString());
            });
          } else {
            this.$api.system.tzgg.add(this.formData).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
            }).catch((e) => {
              this.$message.error(e.toString());
            });
          }
        } else {
          const firstError = Object.values(validateResult)[0]?.[0]?.message;
          this.$message.error(firstError);
        }
      });
    },
  }
}
</script>

<style scoped></style>
